#Model
#This module is used for retrieving data from the cache.
#
#Written by Kenneth

class View_Manager:

	def __init__(self, input_cache):
		"""The View_Manager assumes that all of the requisite 
		boilerplate for starting the cache reading of the MQ
		has been taken care of."""
		self.the_cache = input_cache
		self.the_server = self.the_cache.get_server_data()
		self.list_names = []
		self.dictionary_torrents = {}
	
	def __generate_list_names__(self):
		self.list_names = self.the_cache.get_torrent_names()
	
	def __generate_list_torrents__(self):
		for name in self.list_names:
			if (name not in self.dictionary_torrents.keys()):
				self.dictionary_torrents.update({name : self.the_cache.get(name)})
	
	def get_server_view(self, data_index, get_update = True):
		"""get_server_view returns a two element list where
		the first element consists of a list of the x-vals and
		the second element consists of a list of the y-vals.
		data_index here is the identifier for the requested data
		from the pre-defined list in the cache. The index mapping
		is provided there."""
		self.__generate_list_names__()
		self.__generate_list_torrents__()
		
		timestamps = []
		server_data = []
	
		timestamps = self.__generate_timestamps__(self.the_server, get_update)
		server_data = self.__generate_data_list__(self.the_server, data_index, get_update)
	
		retList = [timestamps, server_data]
	
		return retList
	
	def get_torrent_view(self, torrent_name, data_index, get_update = True):
		"""same thing as get_server_view, only for torrents"""
		self.__generate_list_names__()
		self.__generate_list_torrents__()
		
		timestamps = []
		torrent_data = []
	
		timestamps = self.__generate_timestamps__(self.dictionary_torrents[str(torrent_name)], get_update)
		torrent_data = self.__generate_data_list__(self.dictionary_torrents[str(torrent_name)], data_index, get_update)
	
		retList = [timestamps, torrent_data]
	
		return retList
	
	def get_peer_info(self, torrent_name, keys):
		"""Returns a list of dictionaries. There is one dictionary
		for each peer containing the requested data. The requested keys 
		should be strings given as a list. The data is from the most recent update."""
		
		torrent = self.the_cache.get(torrent_name)
		peers = torrent.pop().tail().peers
		
		retList = peers
		for i in peers:
			i = {}
			
		for k in keys:
			for (i, peer) in enumerate(peers):
				retList[i].update({k : peer[k]})
		
		return retList
			
	
	def __generate_timestamps__(self, in_linkedlist, get_update = True):
		retList = []
		cur_pos = in_linkedlist.position
		if (cur_pos is None):
			return retList
		if get_update:
			retList.append(in_linkedlist.tail().timestamp)
			return retList
		else:
			in_linkedlist.position = in_linkedlist.head()
		while (in_linkedlist.position is not in_linkedlist.tail()):
			retList.append(in_linkedlist.position.timestamp)
			in_linkedlist.next()
		retList.append(in_linkedlist.tail().timestamp)
		if get_update:
			return retList
		else:
			in_linkedlist.position = cur_pos
			return retList
	
	def __generate_data_list__(self, in_linkedlist, data_index, get_update = True):
		retList = []
		cur_pos = in_linkedlist.position
		if (cur_pos is None):
			return retList
		if get_update:
			return in_linkedlist.tail().data[data_index]
		else:
			in_linkedlist.position = in_linkedlist.head()
		while(in_linkedlist.position is not in_linkedlist.tail()):
			retList.append(in_linkedlist.position.data[data_index])
			in_linkedlist.next()
		retList.append(in_linkedlist.tail().data[data_index])
		if get_update:
			return retList
		else:
			in_linkedlist.position = cur_pos
			return retList